package cn.wmw.xyy.web.controller;

import cn.wmw.xyy.dto.KillCoursePreOrderDto;
import cn.wmw.xyy.service.IKillCourseService;
import cn.wmw.xyy.domain.KillCourse;
import cn.wmw.xyy.query.KillCourseQuery;
import cn.wmw.result.JsonResult;
import cn.wmw.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody KillCourse killCourse){
        killCourseService.saveKillCourse(killCourse);
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JsonResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }


    @RequestMapping(value = "/online/all",method = RequestMethod.GET)
    public JsonResult getOnlineAll(){
        return JsonResult.success(killCourseService.getOnlineAll());
    }


    /**
     * 根据活动Id和秒杀课程Id查询秒杀课程信息
     * @param killCourseId
     * @param killActivityId
     * @return
     */
    @RequestMapping(value = "/online/one/{killCourseId}/{killActivityId}",method = RequestMethod.GET)
    public JsonResult getOnlineOne(@PathVariable("killCourseId")Long killCourseId, @PathVariable("killActivityId")Long killActivityId){
        return JsonResult.success(killCourseService.getOnlineOne(killCourseId, killActivityId));
    }
    /**
     * 秒杀下单接口，秒杀的峰值接口
     * @param killCoursePreOrderDto
     * @return
     */
    @RequestMapping(value = "/kill",method = RequestMethod.POST)
    public JsonResult kill(@RequestBody KillCoursePreOrderDto killCoursePreOrderDto){
        return JsonResult.success(killCourseService.kill(killCoursePreOrderDto));
    }

    /**
     * 获取秒杀订单结算页信息
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "/online/oneByOrder/{orderNo}",method = RequestMethod.GET)
    public JsonResult oneByOrder(@PathVariable("orderNo")String orderNo){
        return JsonResult.success(killCourseService.oneByOrder(orderNo));
    }
}
